package org.cyk.service;

import com.cyk.HelloProto;
import com.cyk.HelloServiceGrpc;
import com.google.protobuf.ProtocolStringList;
import io.grpc.stub.StreamObserver;

public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {

    @Override
    public void helloList(HelloProto.HelloListRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        //1.获取 client 的请求参数
        ProtocolStringList nameList = request.getNameList();
        //2.业务处理
        for(String name : nameList) {
            System.out.println("name: " + name);
        }
        //3.封装响应
        HelloProto.HelloResponse response = HelloProto.HelloResponse
                .newBuilder()
                .setResult("ok!")
                .build();
        //通过这个方法，把响应消息回传给 client
        responseObserver.onNext(response);
        //通知 client，整个服务结束(底层返回一个标记，client 就能监听到)
        responseObserver.onCompleted();
    }

    @Override
    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        //1.接收 client 的请求参数
        String name = request.getName();
        //2.业务处理
        System.out.println("name: " + name);
        //3.封装响应
        HelloProto.HelloResponse response = HelloProto.HelloResponse
                .newBuilder()
                .setResult("ok!") //填充数据
                .build();
        //通过这个方法，把响应消息回传给 client
        responseObserver.onNext(response);
        //通知 client，整个服务结束(底层返回一个标记，client 就能监听到)
        responseObserver.onCompleted();
    }

}
